package com.fzj.example.socket.tcp.nonblock.bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * ClassName: SocketHandler
 * date: 2019/11/18 11:12
 * Description: TODO
 *
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class SocketHandler implements  Runnable {

    private Socket socket;
    private HandlerExecutorPool handlerExecutorPool;
    private String type;

    SocketHandler(){}

    SocketHandler(String type,HandlerExecutorPool handlerExecutorPool,Socket socket){
        this.type=type;
        this.handlerExecutorPool=handlerExecutorPool;
        this.socket=socket;
    }

    @Override
    public void run() {
        try {
            InputStream inputStream=socket.getInputStream();
            OutputStream outputStream=socket.getOutputStream();
            this.handlerExecutorPool.execute(new SendDataHandler(this.type,outputStream));
            this.handlerExecutorPool.execute(new ReceiveDataHandler(inputStream));
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
}
